/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-05-10
 * Time: 16:32
 */
public class Main {
    //day4贪心算法练习力扣121买卖股票最佳时机
    public int maxProfit1(int[] prices) {
        //定义prveMin
        int prveMin = Integer.MAX_VALUE;
        int ret = 0;
        for(int i = 0; i < prices.length; i++) {
            //更新利润
            ret = Math.max(ret,prices[i] - prveMin);
            //更新最小值
            prveMin = Math.min(prveMin,prices[i]);
        }
        return ret;
    }
    //day4贪心算法练习力扣122买卖股票最佳时机||

    //实现方法1：双指针
    public int maxProfit2(int[] prices) {
        int ret = 0;
        for (int i = 0; i < prices.length; i++) {
            int j = i;
            // 通过这个j指针去调度
            while (j + 1 < prices.length && prices[j + 1] > prices[j]) {
                ret += prices[j + 1] - prices[j];
                j++;
            }
            i = j;
        }
        return ret;
    }
    //实现方法2: 拆分一段一段
    public int maxProfit3(int[] prices) {
        int ret = 0;
        for (int i = 1; i < prices.length; i++) {
            if(prices[i] - prices[i-1] > 0) {
                ret += prices[i] -prices[i-1];
            }
        }
        return ret;
    }
}
